<?php

use Phalcon\Mvc\Model\Criteria;
use Phalcon\Paginator\Adapter\Model as Paginator;

/**
 * ProductsController
 *
 * Manage CRUD operations for products
 */
class ProductsController extends ControllerBase
{
    public function initialize()
    {
        $this->tag->setTitle('产品管理');
        parent::initialize();

		$this->tag->setDefault('id', '');
		$this->tag->setDefault('name', '');
		$this->tag->setDefault('product_types_id', '');
		$this->tag->setDefault('price', '');
		$this->view->active = 'N';
    }

    /**
     * Shows the index action
     */
    public function indexAction()
    {
		$this->session->conditions = null;
		$this->view->setVar("productTypes", ProductTypes::find());
    }

    /**
     * Search products based on current criteria
     */
    public function searchAction()
    {
        $numberPage = 1;
        if ($this->request->isPost()) {
            $query = Criteria::fromInput($this->di, "Products", $this->request->getPost());
            $this->persistent->searchParams = $query->getParams();
        } else {
            $numberPage = $this->request->getQuery("page", "int");
        }

        $parameters = array();
        if ($this->persistent->searchParams) {
            $parameters = $this->persistent->searchParams;
        }

        $products = Products::find($parameters);
        if (count($products) == 0) {
            $this->flash->notice("没有搜索到任何产品");
            return $this->forward("products/index");
        }

        $paginator = new Paginator(array(
            "data"  => $products,
            "limit" => 10,
            "page"  => $numberPage
        ));

        $this->view->page = $paginator->getPaginate();
    }

    /**
     * Shows the form to create a new product
     */
    public function newAction()
    {
		$this->view->setVar("productTypes", ProductTypes::find());
		$this->view->active = 'N';
    }

    /**
     * Edits a product based on its id
     */
    public function editAction($id)
    {
        if (!$this->request->isPost()) {

			$this->view->setVar("productTypes", ProductTypes::find());
            $product = Products::findFirstById($id);
            if (!$product) {
                $this->flash->error("找不到相关产品");
                return $this->forward("products/index");
            }

			$this->tag->setDefault('id', $product->id);
			$this->tag->setDefault('name', $product->name);
			$this->tag->setDefault('product_types_id', $product->product_types_id);
			$this->tag->setDefault('price', $product->price);
			$this->view->active = $product->active;
        }
    }

    /**
     * Creates a new product
     */
    public function createAction()
    {
        if (!$this->request->isPost()) {
            return $this->forward("products/index");
        }

        $product = new Products();

        $data = $this->request->getPost();

		if (!isset($data['active']))
			$data['active'] = 'N';

		foreach($data as $key => $value) {
			$product->$key = $value;
		}		

        if ($product->save() == false) {
            foreach ($product->getMessages() as $message) {
                $this->flash->error($message);
            }
            return $this->forward('products/new');
        }

		unset ($_POST);

        $this->flash->success("添加产品成功");
        return $this->forward("products/index");
    }

    /**
     * Saves current product in screen
     *
     * @param string $id
     */
    public function saveAction()
    {
        if (!$this->request->isPost()) {
            return $this->forward("products/index");
        }

        $id = $this->request->getPost("id", "int");

        $product = Products::findFirstById($id);
        if (!$product) {
            $this->flash->error("Product does not exist");
            return $this->forward("products/index");
        }

        $data = $this->request->getPost();
		
		if (!isset($data['active']))
			$data['active'] = 'N';

		foreach($data as $key => $value) {
			$product->$key = $value;
		}		

        if ($product->save() == false) {
            foreach ($product->getMessages() as $message) {
                $this->flash->error($message);
            }
            return $this->forward('products/edit/' . $id);
        }

		unset ($_POST);

        $this->flash->success("产品更新成功");
        return $this->forward("products/index");
    }

    /**
     * Deletes a product
     *
     * @param string $id
     */
    public function deleteAction($id)
    {

        $products = Products::findFirstById($id);
        if (!$products) {
            $this->flash->error("没有找到要删除的产品");
            return $this->forward("products/index");
        }

        if (!$products->delete()) {
            foreach ($products->getMessages() as $message) {
                $this->flash->error($message);
            }
            return $this->forward("products/search");
        }

        $this->flash->success("删除产品成功");
        return $this->forward("products/index");
    }
}
